/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.edit.command;

import static com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil.getInteractionFor;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.uml2.uml.Interaction;
import org.eclipse.uml2.uml.InteractionFragment;

import com.cea.papyrus.core.utils.LookForElement;
import com.cea.papyrus.diagram.sequence.sedi.edit.analysis.CoverableAnalyser;
import com.cea.papyrus.diagram.sequence.sedi.model.command.LifelinePosition;
import com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * Performs geometrical analysis of a diagram in order to detect lifeline
 * covering by interaction fragments.
 * <p>
 * The following attributes must be set before the command can run successfully:
 * </p>
 * <ul>
 * <li>FragmentView
 * <li>EditPart - any EditPart
 * </ul>
 * <p>
 * When completed, the following attributes can be read to access the result:
 * </p>
 * <ul>
 * <li>Covered
 * </ul>
 * 
 * @author frankd@ifi.uio.no
 */
public class AnalyseCoveredCommand extends Command {

	private EditPart anEditPart;
	private GraphNode fragmentView;
	private List<LifelinePosition> covered;

	public void setFragmentView(final GraphNode fragmentView) {
		this.fragmentView = fragmentView;
	}

	public void setEditPart(final EditPart editPart) {
		this.anEditPart = editPart;
	}

	public List<LifelinePosition> getCovered() {
		return covered;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void execute() {
		final Interaction interaction = getInteractionFor(ModelUtil
				.<InteractionFragment> getSemantic(fragmentView));
		List<GraphNode> listInter = LookForElement
				.lookForGraphElementWODiagram(interaction, LookForElement
						.getDiagram(fragmentView), new ArrayList<GraphNode>(1));
		final GraphNode interactionView = listInter.get(0);
		final CoverableAnalyser analyser = new CoverableAnalyser(anEditPart,
				interactionView);
		covered = analyser.findCovered(fragmentView);
	}

	@Override
	public void undo() {
		covered = null;
	}

	@Override
	public void redo() {
		execute();
	}

	@Override
	public void dispose() {
		fragmentView = null;
		covered = null;
		anEditPart = null;
	}
}
